/*----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the 
License at http://www.mozilla.org/MPL/
	
Software distributed under the License is distributed on 
an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, 
either express or implied. 
See the License for the specific language governing rights and 
limitations under the License.
	
The Original Code is Fever RS ( ActionScript 3 ) Framework code.
	
The Initial Developer of the Original Code is Romain Ecarnot.
Portions created by Initial Developer are Copyright (C) 2006
the Initial Developer. All Rights Reserved.
	
Contributor(s):
	
Unless required by applicable law or agreed to in writing, 
software distributed under the License is distributed on 
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
OF ANY KIND, either express or implied.
See the License for the specific language governing permissions 
and limitations under the License.
----------------------------------------------------------------------------*/

package fever.ui.menu 
{
	import flash.events.ContextMenuEvent;
	import flash.ui.ContextMenuItem;
	
	import com.bourre.events.EventBroadcaster;
	
	import fever.events.PopupContextMenuEvent;
	import fever.utils.Stringifier;	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * Dispatched when a user selects an item from a context menu.
	 *
	 * @eventType fever.events.PopupContextMenuEvent.onContextItemSelectEVENT
	 */
	[Event(name="onContextItemSelect", type="fever.events.PopupContextMenuEvent")]
	
		/**
	 * Create custom menu items to display in the Flash Player context menu.
	 * 
	 * <p>Each <code>PopupContextMenuItem</code> object has a caption (text) that is 
	 * displayed in the context menu.<br />
	 * To add a new item to a context menu, you add it to the 
	 * <code>PopupContextMenu.addItem()</code> method.
	 * 
	 * @example
	 * <listing version="3.0">
	 * 
	 * public function test() : void
	 * {
	 * 	var cMenu : PopupContextMenu = new PopupContextMenu();
	 * 	cMenu.addEventListener( PopupContextMenuEvent.onContextMenuSelectEVENT, _onMenuSelect );
	 * 
	 * 	var item : PopupContextMenuItem = cMenu.addItem( 'Show me', 'MyID' );
	 * 	item.addEventListener( PopupContextMenuEvent.onContextItemSelectEVENT, _onItemSelect );
	 * }
	 * </listing>
	 * 
	 * @see fever.ui.menu.PopupContextMenu
	 * @see fever.ui.menu.PopupContextMenu#addItem()
	 * 
	 * @author Romain Ecarnot
	 */
	dynamic public class PopupContextMenuItem
	{
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------
		
		private var _ownerID : String;
		private var _oEB : EventBroadcaster;
		private var _caption : String;		private var _item : ContextMenuItem;
		private var _id : String;
		
		
		//--------------------------------------------------------------------
		// Public properties
		//--------------------------------------------------------------------
		
		/** 
		 * String used to prefix the label item.
		 * 
		 * @default ""
		 */
		public static var PREFIX_ROOT : String = "";
		
		/**
		 * Returns item identifier.
		 */
		public function get id() : String { return _id; }
		
		/**
		 * Specifies the menu item caption (text) displayed in 
		 * the context menu.
		 */
		public function get caption() : String { return _caption; }
		/** @private */
		public function set caption( value : String ) : void
		{ 
			_caption = value;
			_item.caption = PREFIX_ROOT + _caption;
		}
		
		/**
		 * Indicates whether the specified menu item is enabled 
		 * or disabled.
		 */
		public function get enabled() : Boolean { return _item.enabled; }
		/** @private */
		public function set enabled( b : Boolean ) : void
		{ 
			_item.enabled = b;
		}
		
		/**
		 * Indicates whether the specified menu item is visible when the 
		 * Flash Player context menu is displayed.
		 */
		public function get visible() : Boolean { return _item.visible; }
		/** @private */
		public function set visible( b : Boolean ) : void
		{ 
			_item.visible = b;
		}
		
		/**
		 * Indicates whether a separator bar should appear above the 
		 * specified menu item.
		 */
		public function get separatorBefore() : Boolean { return _item.separatorBefore; }
		/** @private */
		public function set separatorBefore( b : Boolean ) : void
		{
			_item.separatorBefore = b;
		}
		
		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Creates new <code>PopupContextMenuItem</code> instance.
		 * 
		 * @param label A Menu item caption (text) displayed in the context 
		 * menu.
		 * @param id Item identifier
		 * @param menuID Menu owner id
		 */
		internal static function create( label : String, id : String, 
			menuID : String ) : PopupContextMenuItem
		{
			return new PopupContextMenuItem( 
				label, 
				id, 
				menuID, 
				new PrivateConstructorAccess() );	
		}
		
		/**
		 * @private
		 * Instance are created using PopupContextMenu#addItem() method.
		 * 
		 * @param label A Menu item caption (text) displayed in the context 
		 * menu.
		 * @param id Item identifier
		 * @param menuID Menu owner id
		 */
		function PopupContextMenuItem( label : String, id : String, 
			menuID : String, access : PrivateConstructorAccess )
		{
			_ownerID = menuID;
			_oEB = new EventBroadcaster( this );
			
			_caption = label;
			_item = new ContextMenuItem( PREFIX_ROOT + _caption );
			_item.addEventListener( 
				ContextMenuEvent.MENU_ITEM_SELECT, 
				_onItemSelect 
			);
		}
		
		/**
		 * Adds <code>listener</code> to receive <code>type</code> event type.
		 * 
		 * @param type Event type
		 * @param listener Listener object
		 * @param rest Optional parameters ( Delegation process )
		 */
		public function addEventListener( type : String, listener : Object
			, ... rest ) : Boolean
		{
			return _oEB.addEventListener.apply( _oEB, rest.length > 0 
				? [ type, listener ].concat( rest ) 
				: [ type, listener ] 
			);
		}
		
		/**
		 * Removes <code>listener</code> from receiving <code>type</code> 
		 * event type.
		 * 
		 * @param type Event type
		 * @param listener Listener object
		 */
		public function removeEventListener( type : String, listener : Object ) 
			: Boolean
		{
			return _oEB.removeEventListener( type, listener );
		}
		
		/**
		 * Removes all item event listeners.
		 */
		public function removeAllListeners() : void
		{
			_oEB.removeAllListeners( );
		}
		
		/**
		 * Returns ContextMenuItem object.
		 */
		public function getFlashItem() : ContextMenuItem
		{
			return _item;
		}
				
		/**
		 * Returns string representation.
		 */
		public function toString() : String
		{
			return Stringifier.parse( this, _item.caption );
		}
		
		
		//--------------------------------------------------------------------
		// Private implementations
		//--------------------------------------------------------------------
		
		private function _onItemSelect( event : ContextMenuEvent ) : void
		{
			var e : PopupContextMenuEvent = new PopupContextMenuEvent(
				PopupContextMenuEvent.onContextItemSelectEVENT, 
				PopupContextMenuLocator.get( _ownerID ), 
				this 
			);
			
			_oEB.broadcastEvent( e );
		}		
	}
}

internal class PrivateConstructorAccess 
{
}
